package io.renren.modules.task.entity;

import com.sun.jna.WString;
import io.renren.common.utils.SpringContextUtils;
import io.renren.modules.task.controller.TestBean;
import ioserver.IOServerApi;
import ioserver.IOServerService;
import org.apache.tomcat.util.bcel.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/11 16:07
 * @Version 1.0
 */
public class PLCThread extends Thread{
    //todo 写入日志（结束日志）,开始日志就在外面写好了。 在启动线程监听的时候，必须先和plc连接成功
    //todo 在全局new 一个Thread,否则new 出许多线程占用资源。

    private final static Logger logger = LoggerFactory.getLogger(SalveThread.class);
    IOServerService ioServerService = (IOServerService) SpringContextUtils.getBean("iOServerService");

    IOServerApi ioServerApi = (IOServerApi) SpringContextUtils.getBean("iOServerApi");


    /**
     *当前层
     **/
    private com.ioserver.bean.Union_DataType.ByValue wscurrentLayer;
    /**
     *当前列（分为A、B）
     **/
    private com.ioserver.bean.Union_DataType.ByValue wscurrentColumn;
    /**
     *当前位置
     **/
    private com.ioserver.bean.Union_DataType.ByValue wscurrentPosition;

    private com.ioserver.bean.Union_DataType.ByValue wswalkingRun;


    /**
     * 常量
     **/
    private final WString currentlayer = new WString("Current_Layer");
    private final WString currentcol = new WString("Current_Column");
    private final WString currentpos = new WString("Current_Layer");
    private final WString walkingrun = new WString("Walking_Run");


    /**
     *  目的地
     **/
    /**
     *目标层
     **/
    private Short destinationLayer = null;
    /**
     *目标列
     **/
    private Short destinationColumn = null;
    /**
     *取货位(一个位置 对应两列)
     **/
    private Short takePosition = null;



    public PLCThread(Short destinationLayer,Short destinationColumn,Short takePosition){
        this.destinationLayer = destinationLayer;
        this.destinationColumn = destinationColumn;
        this.takePosition = takePosition;
    }

    boolean flags = true;
    int count = 0;
    int newCount = 0;

    @Override
    public void run(){
        while (true){
            while (flags){
                logger.info("i am running");
                wswalkingRun = ioServerApi.funcGetTagValue(walkingrun).TagValue.TagValue;
                //todo 看看bitVal是否为 boolen值
                if(!wswalkingRun.bitVal){
                    //false 表示为 停止
                    try {
                        wscurrentLayer = ioServerApi.funcGetTagValue(currentlayer).TagValue.TagValue;
                        wscurrentColumn = ioServerApi.funcGetTagValue(currentcol).TagValue.TagValue;
                        wscurrentPosition = ioServerApi.funcGetTagValue(currentpos).TagValue.TagValue;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    if(wscurrentLayer.i2Val.equals(destinationLayer) && wscurrentColumn.i2Val.equals(destinationColumn)){
                        //todo  放货成功后，写入日志（plc点表缺乏一个 完成任务成功的标志）
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.err.println(Thread.currentThread().getName()+"-线程意外退出了");
                    break;
                }
            }
            try {
                // 注意这里让线程 停顿个 0.1s很重要，当flags为false ，那就剩下 while(true){} 空跑了，这个线程虽然不会执行完但是会宕机down掉
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //todo 结束 -- 记录
    }

    public void shutdown(){
        this.flags = false;
    }
    public void wakeup(){
        this.flags = true;
    }

}
